namespace Crux.Core.Dapper.Test;
public partial class UnitOfWorkTest
{
    [Fact]
    public void DeleteTest()
    {
        var model = repository.FirstOrDefault<TestModel>(c => c.Id > 0);

        Assert.NotNull(model);

        unitOfWork.Delete(model);
    }
    [Fact]
    public void DeleteWithTransactionRollbackTest()
    {
        using var transaction = unitOfWork.BeginTransaction();

        var model = repository.FirstOrDefault<TestModel>(c => c.Id > 0);

        Assert.NotNull(model);

        unitOfWork.Delete(model);

        transaction.Rollback();
    }
    [Fact]
    public void DeleteWithTransactionCommitTest()
    {
        using var transaction = unitOfWork.BeginTransaction();

        var model = repository.FirstOrDefault<TestModel>(c => c.Id > 0);

        Assert.NotNull(model);

        unitOfWork.Delete(model);

        transaction.Commit();
    }
    [Fact]
    public async Task DeleteAsyncTest()
    {
        var model = await repository.FirstOrDefaultAsync<TestModel>(c => c.Id > 0).ConfigureAwait(false);

        Assert.NotNull(model);

        await unitOfWork.DeleteAsync(model).ConfigureAwait(false);
    }
    [Fact]
    public async Task DeleteAsyncWithTransactionRollbackTest()
    {
        using var transaction = unitOfWork.BeginTransaction();

        var model = await repository.FirstOrDefaultAsync<TestModel>(c => c.Id > 0).ConfigureAwait(false);

        Assert.NotNull(model);

        await unitOfWork.DeleteAsync(model).ConfigureAwait(false);

        transaction.Rollback();
    }
    [Fact]
    public async Task DeleteAsyncWithTransactionCommitTest()
    {
        using var transaction = unitOfWork.BeginTransaction();

        var model = await repository.FirstOrDefaultAsync<TestModel>(c => c.Id > 0).ConfigureAwait(false);

        Assert.NotNull(model);

        await unitOfWork.DeleteAsync(model).ConfigureAwait(false);

        transaction.Commit();
    }
    [Fact]
    public void DeleteWithPredicateTest()
    {
        int effectRow = unitOfWork.Delete<TestModel>(c => c.Id == 0);

        Assert.Equal(0, effectRow);
    }
    [Fact]
    public void DeleteWithPredicateAndTransactionRollbackTest()
    {
        using var transaction = unitOfWork.BeginTransaction();

        int effectRow = unitOfWork.Delete<TestModel>(c => c.Id == 0);

        Assert.Equal(0, effectRow);

        transaction.Rollback();
    }
    [Fact]
    public void DeleteWithPredicateAndTransactionCommitTest()
    {
        using var transaction = unitOfWork.BeginTransaction();

        int effectRow = unitOfWork.Delete<TestModel>(c => c.Id == 0);

        Assert.Equal(0, effectRow);

        transaction.Commit();
    }
    [Fact]
    public void DeleteRangeTest()
    {
        var model = repository.Query<TestModel>(p => p.Id > 0).FirstOrDefault();

        Assert.NotNull(model);

        unitOfWork.DeleteRange(new List<TestModel> { model });
    }

    [Fact]
    public void DeleteRangeWithTransactionRollbackTest()
    {
        using var transaction = unitOfWork.BeginTransaction();
        var model = repository.Query<TestModel>(p => p.Id > 0).FirstOrDefault();
        Assert.NotNull(model);

        int effectRow = unitOfWork.DeleteRange(new List<TestModel> { model });
        transaction.Rollback();
    }
    [Fact]
    public void DeleteRangeWithTransactionCommitTest()
    {
        using var transaction = unitOfWork.BeginTransaction();
        var model = repository.Query<TestModel>(p => p.Id > 0).FirstOrDefault();
        Assert.NotNull(model);

        int effectRow = unitOfWork.DeleteRange(new List<TestModel> { model });
        transaction.Commit();
    }
    [Fact]
    public async Task DeleteAsyncWithPredicateTest()
    {
        int effectRow = await unitOfWork.DeleteAsync<TestModel>(c => c.Id == 0).ConfigureAwait(false);

        Assert.Equal(0, effectRow);
    }
    [Fact]
    public async Task DeleteAsyncWithPredicateAndTransactionRollbackTest()
    {
        using var transaction = unitOfWork.BeginTransaction();

        int effectRow = await unitOfWork.DeleteAsync<TestModel>(c => c.Id == 0).ConfigureAwait(false);

        Assert.Equal(0, effectRow);

        transaction.Rollback();
    }
    [Fact]
    public async Task DeleteAsyncWithPredicateAndTransactionCommitTest()
    {
        using var transaction = unitOfWork.BeginTransaction();

        int effectRow = await unitOfWork.DeleteAsync<TestModel>(c => c.Id == 0).ConfigureAwait(false);

        Assert.Equal(0, effectRow);

        transaction.Commit();
    }
}